/*
 * 文件名：DistrictInfoServiceImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2015年7月14日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.system.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.system.DistrictInfo;
import com.zcsy.manage.persistence.system.DistrictInfoDao;
import com.zcsy.manage.service.system.IDistrictInfoService;

/**
 * @Title: DistrictInfoServiceImpl.java
 * @Package： com.zcsy.shop.service.common.impl 
 * @Description: 区域信息service层接口实现类
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年7月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class DistrictInfoServiceImpl implements IDistrictInfoService {

    @Autowired
    private DistrictInfoDao districtInfoDao;
    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#getDistrictInfoByKeyWord(com.zcsy.shop.model.common.DistrictInfo)
     */
    @Override
    public List<DistrictInfo> getDistrictInfoByKeyWord(DistrictInfo districtInfo) {
        return districtInfoDao.getDistrictInfoByKeyWord(districtInfo);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#insert(com.zcsy.shop.model.common.DistrictInfo)
     */
    @Override
    public int insert(DistrictInfo districtInfo) {
        return districtInfoDao.insert(districtInfo);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#updateByPrimaryKey(com.zcsy.shop.model.common.DistrictInfo)
     */
    @Override
    public int updateByPrimaryKey(DistrictInfo districtInfo) {
        return districtInfoDao.updateByPrimaryKey(districtInfo);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#deleteByPrimaryKeys(java.lang.Integer[])
     */
    @Override
    public int deleteByPrimaryKeys(Integer[] ids) {
        int result = Constants.SQL_RESULT_FAILD;
        List<DistrictInfo> districtInfos = districtInfoDao.getDistrictInfoByParentId(Arrays.asList(ids));
        if(CollectionUtils.isNotEmpty(districtInfos)){
            result = Constants.SQL_RESULT_ERROR;
        }else{
            result = districtInfoDao.deleteByPrimaryKeys(ids);
        }
        return result;
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#getDistrictInfoByLevel(java.lang.Integer)
     */
    @Override
    public List<DistrictInfo> getDistrictInfoByLevel(Integer level) {
        return districtInfoDao.getDistrictInfoByLevel(level);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#getDistrictInfoByParentId(java.util.List)
     */
    @Override
    public List<DistrictInfo> getDistrictInfoByParentName(String parentName,Integer level) {
        Map<String , Object> map = new HashMap<String, Object>();
        map.put("parentName", parentName);
        map.put("regionLevel", level);
        DistrictInfo parent = districtInfoDao.getDistrictInfoByName(map);
        List<Integer> list = new ArrayList<Integer>();
        list.add(parent.getId());
        return districtInfoDao.getDistrictInfoByParentId(list);
    }
    
    @Override
    public List<DistrictInfo> getDistrictInfoByParentId(Integer parentId) {
    	List<Integer> list = new ArrayList<Integer>();
    	list.add(parentId);
    	return districtInfoDao.getDistrictInfoByParentId(list);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#getDistrictInfoById(java.lang.Integer)
     */
    @Override
    public DistrictInfo getDistrictInfoById(Integer id) {
        return districtInfoDao.getDistrictInfoById(id);
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#saveOrUpdateDistrictInfo(com.zcsy.shop.model.common.DistrictInfo)
     */
    @Override
    public void saveOrUpdateDistrictInfo(DistrictInfo districtInfo) {
      //当设置分类为一级分类时，清空其父级分类ID
        if(districtInfo.getRegionLevel()== Constants.DISTRICT_LEVEL_FIRST){
            districtInfo.setParentId(0);
        }
        
        if(districtInfo!=null && null != districtInfo.getId()){
            districtInfoDao.updateByPrimaryKey(districtInfo);
        }else{
            districtInfo.setState(Constants.STATE_ENABLED);
            districtInfoDao.insert(districtInfo);
        } 
    }

    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#getAllDistrictInfo()
     */
    @Override
    public List<DistrictInfo> getAllDistrictInfo() {
        List<DistrictInfo> districtInfos = districtInfoDao.getDistrictInfoByLevel(Constants.DISTRICT_LEVEL_FIRST);
        List<DistrictInfo> result = new ArrayList<DistrictInfo>();
        if(CollectionUtils.isNotEmpty(districtInfos)){
            for(DistrictInfo district:districtInfos){
                DistrictInfo districtInfo = getSubType(district);
                 result.add(districtInfo);
            }
        }
        
        return result;
    }

    /**
     * @Description 递归获取区域信息的子类<br>
     * 
     * @param districtInfo 区域信息对象
     * @return  获取区域信息后的区域信息对象
     * @author caiyinli 13277030520@163.com
     * @date 2015年7月14日
     * @see 
     */
    private DistrictInfo getSubType(DistrictInfo districtInfo){
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(districtInfo.getId());
        List<DistrictInfo> subType = districtInfoDao.getDistrictInfoByParentId(ids);
        List<DistrictInfo> result = new ArrayList<DistrictInfo>();
        if(CollectionUtils.isNotEmpty(subType)){
            for(DistrictInfo  disInfo:subType){
                DistrictInfo sub  = getSubType(disInfo);
                result.add(sub);
            }
        }
        districtInfo.setSubTypes(result);
        return districtInfo;
    }
    /* (non-Javadoc)
     * @see com.zcsy.shop.service.common.IDistrictInfoService#updateDistrictInfoState(int, int)
     */
    @Override
    public int updateDistrictInfoState(int id, int state) {
        int result = Constants.SQL_RESULT_FAILD;
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(id);
        List<DistrictInfo> districtInfos = districtInfoDao.getDistrictInfoByParentId(ids);
        if(CollectionUtils.isNotEmpty(districtInfos)){
            result = Constants.SQL_RESULT_ERROR;
        }else{
            DistrictInfo districtInfo = new DistrictInfo();
            districtInfo.setId(id);
            if(state == Constants.STATE_ENABLED){
                districtInfo.setState(Constants.STATE_DISABLED);
            }else{
                districtInfo.setState(Constants.STATE_ENABLED);
            }
            result = districtInfoDao.updateByPrimaryKey(districtInfo);
        }
        return result;
    }

    @Override
    public DistrictInfo getDistrictInfoByName(String parentName, Integer level) {
        Map<String , Object> map = new HashMap<String, Object>();
        map.put("parentName", parentName);
        map.put("regionLevel", level);
        
        return districtInfoDao.getDistrictInfoByName(map);
    }

	@Override
	public List<DistrictInfo> getProvincesAndcities() {
		return districtInfoDao.getProvincesAndcities();
	}

}
